package com.andnnl.template.param;

import com.andnnl.template.ParamTemplateManage;
import com.fasterxml.jackson.databind.JsonNode;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Random;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;

/**
 * Created by chenss on 2025/7/8.
 */
// 日期类型配置
public class DateParamConfig extends BaseParamConfig {
    String dateFormat;
    long minDate;
    long maxDate;

    @Override
    public void validate() {
        if (dateFormat == null || dateFormat.trim().isEmpty())
            dateFormat = "yyyy-MM-dd";
        else {
            try {
                new SimpleDateFormat(dateFormat);
            } catch (Exception e) {
                throw new IllegalArgumentException("日期格式无效: " + dateFormat, e);
            }
        }
        if (minDate > maxDate)
            throw new IllegalArgumentException("日期范围无效: " + name);
    }

    @Override
    public String generateValue() {
        Random rand = new Random();
        long diff = maxDate - minDate;
        long randomTime = minDate + (long) (rand.nextDouble() * diff);
        return new SimpleDateFormat(dateFormat).format(new Date(randomTime));
    }

    public String getDateFormat() {
        return dateFormat;
    }

    public void setDateFormat(String dateFormat) {
        this.dateFormat = dateFormat;
    }

    public long getMinDate() {
        return minDate;
    }

    public void setMinDate(long minDate) {
        this.minDate = minDate;
    }

    public long getMaxDate() {
        return maxDate;
    }

    public void setMaxDate(long maxDate) {
        this.maxDate = maxDate;
    }

    @Override
    public void fromJson(JsonNode node) {
        this.dateFormat = node.path("format").asText("yyyy-MM-dd");
        if (node.has("minDate")) {
            this.minDate = parseDateValue(node.path("minDate").asText());
        }
        if (node.has("maxDate")) {
            this.maxDate = parseDateValue(node.path("maxDate").asText());
        }
    }

    private long parseDateValue(String dateStr) {
        try {
            if (dateStr.matches("\\d+")) {
                return Long.parseLong(dateStr);
            } else {
                return DateUtil.parseDate(dateStr).getTime();
            }
        } catch (Exception e) {
            throw new IllegalArgumentException("日期格式无效: " + dateStr, e);
        }
    }

    @Override
    public boolean validateValue(String value) throws IllegalArgumentException {
        if (StrUtil.isBlank(value)) {
            throw new IllegalArgumentException("日期参数" + name + "不能为空");
        }
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
            long dateTime = sdf.parse(value).getTime();
            if (minDate > 0) {
                if (dateTime < minDate) {
                    throw new IllegalArgumentException(
                            "日期参数" + name + "必须大于" + sdf.format(new Date(minDate)) + "，实际值：" + value);
                }
            }
            if (maxDate > 0) {
                if (dateTime > maxDate) {
                    throw new IllegalArgumentException(
                            "日期参数" + name + "必须小于" + sdf.format(new Date(maxDate)) + "，实际值：" + value);
                }
            }
            return true;
        } catch (Exception e) {
            throw new IllegalArgumentException("参数" + name + "不是有效的日期: " + value, e);
        }
    }

}